﻿// //------------------------------------------------------------------------------
// // <copyright file="" company="Dascoba Development">
// //  Copyright © Dascoba Development 2010
// // </copyright>
// //------------------------------------------------------------------------------
using System;

namespace Dascoba.Umb.FileManager.Dialogs
{
    using System.IO;

    using Dascoba.Umb.FileManager.Config;
    using Dascoba.Umb.FileManager.Support;

    using umbraco;

    /// <summary>
    ///   Upload File
    /// </summary>
    public partial class DeleteItem : umbraco.BasePages.UmbracoEnsuredPage
    {
        /// <summary>
        ///   Handles the Load event of the Page control.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DeleteFileOrFolder(true);
        }

        /// <summary>
        ///   Deletes the file or folder.
        /// </summary>
        /// <param name = "recycle">if set to <c>true</c> [recycle].</param>
        /// <returns></returns>
        private void DeleteFileOrFolder(bool recycle)
        {
            var item = this.Request["item"];
            var type = this.Request["type"];

            if (item == null)
            {
                this.litStatus.Text = ui.Text("fileman", "nothingToDelete");
                return;
            }

            var itemPath = Util.MapPath(item);

            FileSystemInfo fileSystemInfo = null;
            try
            {
                string itemName;

                var error = string.Empty;

                switch (type)
                {
                    case "folder":
                        {
                            fileSystemInfo = new DirectoryInfo(itemPath);

                            itemName = fileSystemInfo.Name;

                            if (recycle)
                            {
                                error = MoveToRecycleBin((DirectoryInfo)fileSystemInfo);
                            }
                            else
                            {
                                try
                                {
                                    ((DirectoryInfo)fileSystemInfo).Delete(true);
                                }
                                catch (Exception ex)
                                {
                                    error = ex.Message;
                                }
                            }
                        }
                        break;
                    case "file":
                        {
                            fileSystemInfo = new FileInfo(itemPath);

                            itemName = fileSystemInfo.Name;

                            if (recycle)
                            {
                                error = MoveToRecycleBin((FileInfo)fileSystemInfo);
                            }
                            else
                            {
                                try
                                {
                                    fileSystemInfo.Delete();
                                }
                                catch (Exception ex)
                                {
                                    error = ex.Message;
                                }
                            }
                        }
                        break;
                    default:
                        {
                            this.litStatus.Text = ui.Text("fileman", "nothingToDelete");
                            return;
                        }
                }

                if (error == string.Empty)
                {
                    this.imgStatus.ImageUrl = Constants.FileManagerGlobalUrl + "/images/success.png";
                    this.litStatus.Text = ui.Text("fileman", "wasDeleted", itemName, CurrentUser);
                }
                else
                {
                    this.imgStatus.ImageUrl = string.Format("{0}/images/fail.png", Constants.FileManagerGlobalUrl);
                    this.litStatus.Text = ui.Text("fileman", "error", error, CurrentUser);
                }
            }
            catch (Exception ex)
            {
                if (fileSystemInfo != null)
                {
                    this.litStatus.Text = ui.Text(
                        "fileman", "errorDeleting", fileSystemInfo.FullName, CurrentUser);
                }
                this.imgStatus.ImageUrl = string.Format("{0}/images/fail.png", Constants.FileManagerGlobalUrl);
                this.litStatus.Text = ui.Text("fileman", "error", ex.Message, CurrentUser);
            }
            return;
        }

        /// <summary>
        /// Moves file to recycle bin.
        /// </summary>
        /// <param name="fileInfo">The file info.</param>
        /// <returns>Error Message</returns>
        private static string MoveToRecycleBin(FileInfo fileInfo)
        {
            try
            {
                var recycleBinItemPath = RecycleBinConfig.AddItem(RecycledItemType.File, fileInfo.FullName, CurrentUser);

                if (recycleBinItemPath != string.Empty)
                {
                    fileInfo.MoveTo(recycleBinItemPath);
                    return string.Empty;
                }
                return "Cannot locate recycle bin";
            }
            catch (Exception ex)
            {
                return ex.Message;

            }
        }

        /// <summary>
        /// Moves directory to recycle bin.
        /// </summary>
        /// <param name="directoryInfo">The directory info.</param>
        /// <returns>Error Message</returns>
        private static string MoveToRecycleBin(DirectoryInfo directoryInfo)
        {
            try
            {
                var recycleBinItemPath = RecycleBinConfig.AddItem(
                    RecycledItemType.Folder, directoryInfo.FullName, CurrentUser);

                if (recycleBinItemPath != string.Empty)
                {
                    directoryInfo.MoveTo(recycleBinItemPath);
                    return string.Empty;
                }
                return "Cannot locate recycle bin";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}